import 'dart:ui';

import 'package:ble_helper_plugin/library.dart';
import 'package:ble_helper_plugin/src/ble_manager.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

// 蓝牙连接助手类
class BluetoothConnectHelper<T extends BluetoothUuidAdapterBase> {
  // 蓝牙适配器
  final List<T> bluetoothAdapter;

  // 构造函数
  BluetoothConnectHelper(this.bluetoothAdapter);

  // 获取蓝牙连接状态
  Stream<CustomBluetoothConnectState> get bluetoothConnectState =>
      _manager.bluetoothConnectState;

  // 获取已连接设备
  BluetoothDevice? get connectedDevice => _manager.connectedDevice;

  // 获取蓝牙管理器
  final BluetoothManager _manager = BluetoothManager();

  // 搜索【目标蓝牙设备】(identyId: 设备标识remoteId 或 广播名称 或 广播报文中的SN)
  Future<bool> search(
    String identyId, {
    Duration? timeout,
    VoidCallback? onTimeout,
    Function(CustomScanResult)? callbackScanResults,
  }) async {
    return _manager.search(
      identyId,
      timeout: timeout,
      onTimeout: onTimeout,
      callbackScanResults: callbackScanResults,
    );
  }

  // 开始扫描
  Future<void> startScan({
    Duration? timeout,
    VoidCallback? onTimeout,
    List<Guid>? serviceUuids,
    CallbackScanResults? callbackScanResults,
    bool filterDuplicate = true,
  }) {
    return _manager.startScan(
      timeout: timeout,
      onTimeout: onTimeout,
      serviceUuids: serviceUuids,
      callbackScanResults: callbackScanResults,
      filterDuplicate: filterDuplicate,
    );
  }

  // 连接蓝牙设备
  Future<bool> connect(
    BluetoothDevice device, {
    Duration? timeout,
    VoidCallback? onTimeout,
  }) {
    return _manager.connectDevice(
      device,
      bluetoothAdapter,
      timeout: timeout,
      onTimeout: onTimeout,
    );
  }

  // 停止扫描
  Future<void> stopScan({
    bool isSkipIdleState = false,
  }) {
    return _manager.stopScan(isSkipIdleState: isSkipIdleState);
  }

  // 断开蓝牙设备
  Future<void> disconnect() => _manager.disconnect();
}
